package com.xjtu.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xjtu.project.common.utls.SessionUtilsUser;
import com.xjtu.project.model.domain.Address;
import com.xjtu.project.service.AddressService;
import com.xjtu.project.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86185
* @description 针对表【address】的数据库操作Service实现
* @createDate 2025-07-04 17:16:44
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Integer addAdress(Address address) {
        Integer id = SessionUtilsUser.getCurrentUserInfo().getId();
        address.setUserId(id);
        return addressMapper.insert(address);
    }

    @Override
    public List<Address> getByUserId() {
        Integer userId = SessionUtilsUser.getCurrentUserInfo().getId();
        return addressMapper.selectByUserId(userId);
    }

    @Override
    public Address getOneMy() {
         Integer userId = SessionUtilsUser.getCurrentUserInfo().getId();
        return addressMapper.selectOneByUserId(userId);
    }

    @Override
    public boolean updateByIdMy(Address address) {
        return addressMapper.updateByIdMy(address);
    }

    @Override
    public void setDefaultAddress(Integer id) {
        //把id的地址设为默认
        //先把当前用户的所有地址设为非默认
        lambdaUpdate().eq(Address::getUserId, SessionUtilsUser.getCurrentUserInfo().getId()).set(Address::getDefaulted, 0).update();
        lambdaUpdate().eq(Address::getId, id).set(Address::getDefaulted, 1).update();
    }
}




